package org.adorsys.aderp.orga.web;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.adorsys.aderp.orga.domain.EmailAddressRole;
import org.adorsys.aderp.orga.domain.OrgaAddressRole;
import org.adorsys.aderp.orga.domain.OrgaPerson;
import org.adorsys.aderp.orga.domain.OrgaResource;
import org.adorsys.aderp.orga.domain.PersonAddress;
import org.adorsys.aderp.orga.domain.ResourceAddress;
import org.adorsys.aderp.orga.service.OrgaAddressRoleService;
import org.adorsys.aderp.orga.service.OrgaAddressService;
import org.adorsys.aderp.orga.service.OrgaResourceService;
import org.adorsys.aderp.orga.service.ResourceAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.roo.addon.web.mvc.controller.scaffold.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@RequestMapping("/resourceaddresses")
@Controller
@RooWebScaffold(path = "resourceaddresses", formBackingObject = ResourceAddress.class)
public class ResourceAddressController {
    @RequestMapping(params = "form", produces = "text/html")
    public String createForm(@RequestParam(value="orgaResourceId", required=false)Long orgaResourceId,
    		@RequestParam(value="addressRoleId", required=false)Long addressRoleId,
    		Model uiModel) {
        populateCreateForm(uiModel, new ResourceAddress(), orgaResourceId, addressRoleId);
        List<String[]> dependencies = new ArrayList<String[]>();
        if (orgaAddressService.countAllOrgaAddresses() == 0) {
            dependencies.add(new String[] { "orgaresources", "orgaresource" });
        }
        if (orgaAddressRoleService.countAllOrgaAddressRoles() == 0) {
            dependencies.add(new String[] { "orgaaddressrole", "orgaaddressroles" });
        }
        if (orgaAddressService.countAllOrgaAddresses() == 0) {
            dependencies.add(new String[] { "orgaaddress", "orgaaddresses" });
        }
        uiModel.addAttribute("dependencies", dependencies);
        return "resourceaddresses/create";
    }

    @RequestMapping(produces = "text/html")
    public String list(@RequestParam(value="orgaResourceId", required=false)Long orgaResourceId,
    		@RequestParam(value="addressRoleId", required=false)Long addressRoleId,
    		@RequestParam(value = "page", required = false) Integer page, 
    		@RequestParam(value = "size", required = false) Integer size, Model uiModel) {

    	OrgaResource orgaResource = orgaResourceId!=null? orgaResourceService.findOrgaResource(orgaResourceId):null;
		OrgaAddressRole addressRole = addressRoleId!=null?orgaAddressRoleService.findOrgaAddressRole(addressRoleId):null;

		Page<ResourceAddress> resourceAddressPage = resourceAddressService.searchResourceAddress(orgaResource, addressRole, page, size);
		uiModel.addAttribute("orgaaddresses", resourceAddressPage.getContent());
		uiModel.addAttribute("maxPages", resourceAddressPage.getNumberOfElements());
    	

        addDateTimeFormatPatterns(uiModel);
        return "resourceaddresses/list";
    }

    void populateCreateForm(Model uiModel, ResourceAddress resourceAddress,Long orgaResourceId,Long addressRoleId) {
    	OrgaResource orgaResource = orgaResourceId!=null? orgaResourceService.findOrgaResource(orgaResourceId):null;
		OrgaAddressRole addressRole = addressRoleId!=null?orgaAddressRoleService.findOrgaAddressRole(addressRoleId):null;
		resourceAddress.setOrgaResource(orgaResource);
		resourceAddress.setAddressRole(addressRole);
        uiModel.addAttribute("resourceAddress", resourceAddress);
        addDateTimeFormatPatterns(uiModel);
        
        if(addressRole!=null){
        	uiModel.addAttribute("orgaaddressroles", Collections.singleton(addressRole));
        } else {
        	uiModel.addAttribute("orgaaddressroles", orgaAddressRoleService.findAllOrgaAddressRoles());
        }
        if(orgaResource!=null){
        	uiModel.addAttribute("orgaresources", Collections.singleton(orgaResource));
        } else {
        	uiModel.addAttribute("orgaresources", orgaResourceService.findAllOrgaResources());
        }
        uiModel.addAttribute("orgaaddresses", orgaAddressService.findAllOrgaAddresses());
    }
}
